package com.example.project.service.impl;

import com.example.project.dao.IMenuDao;
import com.example.project.entity.Menu;
import com.example.project.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *作者：金照富
 * 2023年11月3日
 */
@Service
public class IMenuServiceImpl implements IMenuService {

    @Autowired
    private IMenuDao menuMapper;
    @Override
    public int insertMenu(Menu menu) {
        int menuValue;
        Menu menu1=new Menu();
        menu1.setMenuName(menu.getMenuName());
        List<Menu> allMenu=menuMapper.getAllMenu(menu1);
        if(allMenu.size()>=1){
            menuValue = -1;
        }else {
            menuValue= menuMapper.insertMenu(menu);
        }
        return menuValue;
    }

    @Override
    public int deleteMenu(Menu menu) {
        int menuValue;
        Menu menu1=new Menu();
        menu1.setMenuName(menu.getMenuName());
        List<Menu> allMenu=menuMapper.getAllMenu(menu1);
        if(allMenu.size()==0){
            menuValue = -1;
        }else {
            menuValue= menuMapper.deleteMenu(menu);
        }
        return menuValue;
    }

    @Override
    public int updateMenu(Menu menu) {
        int menuValue;
        Menu menu1=new Menu();
        menu1.setMenuName(menu.getMenuName());
        List<Menu> allMenu=menuMapper.getAllMenu(menu1);
        if(allMenu.size()>=1){
            menuValue = -1;
        }else {
            menuValue= menuMapper.updateMenu(menu);
        }
        return menuValue;
    }

    @Override
    public List<Menu> getAllMenu(Menu menu) {
        return menuMapper.getAllMenu(menu);
    }

    @Override
    public Menu getMenu(Menu menu) {
        return menuMapper.getMenu(menu);
    }
}
